#include "gt_theme.h"

// ok
static int get_color(const char *color) {
  if (!strcmp(color, "yellow"))
    return COLOR_YELLOW;
  else if (!strcmp(color, "red"))
    return COLOR_RED;
  else if (!strcmp(color, "cyan"))
    return COLOR_CYAN;
  else if (!strcmp(color, "green"))
    return COLOR_GREEN;
  else if (!strcmp(color, "black"))
    return COLOR_BLACK;
  else if (!strcmp(color, "blue"))
    return COLOR_BLUE;
  else if (!strcmp(color, "white"))
    return COLOR_WHITE;
  else if (!strcmp(color, "magenta"))
    return COLOR_MAGENTA;

  return -1;
}

// tocheck
theme_t *read_theme(configuration_t *config) {
  const char *buffer;
  char line_color[64];
  char line_number_color[64];
  char highlight_color[64];
  char file_color[64];
  char opened_line_color[64];
  theme_t *new_theme = (theme_t *)calloc(1, sizeof(theme_t));

  load_config(config);
  config_t cfg = get_config(config);

  if (!config_lookup_string(&cfg, "line_color", &buffer)) {
    fprintf(stderr, "greptui: no line_color string found\n");
    goto exit_read_theme;
  }

  // TODO:

  new_theme->line_color = get_color(line_color);
  new_theme->line_number_color = get_color(line_number_color);
  new_theme->opened_line_color = get_color(opened_line_color);
  new_theme->highlight_color = get_color(highlight_color);
  new_theme->file_color = get_color(file_color);
  return new_theme;

exit_read_theme:
  config_destroy(&cfg);
  destroy_theme(new_theme);
  return NULL;
}

// ok
void apply_theme(theme_t *thme) {
  init_pair(COLOR_LINE, thme->line_color, -1);
  init_pair(COLOR_LINE_NUMBER, thme->line_number_color, -1);
  init_pair(COLOR_OPENED_LINE, thme->opened_line_color, -1);
  init_pair(COLOR_HIGHLIGHT, thme->highlight_color, -1);
  init_pair(COLOR_FILE, thme->file_color, -1);
}

// ok
void destroy_theme(theme_t *thme) { free(thme); }